package com.qili.service.it.impl;

import com.qili.base.mapper.BaseMapper;
import com.qili.base.service.impl.BaseServiceImpl;
import com.qili.entity.it.FileCollect;
import com.qili.entity.supervisor.SupervisorFile;
import com.qili.mapper.it.FileCollectMapper;
import com.qili.mapper.it.ItFileMapper;
import com.qili.service.it.ItFileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author liuyang
 * @date 2021/3/18
 * @description
 */
@Service
public class ItFileServiceImpl extends BaseServiceImpl<SupervisorFile,String> implements ItFileService {

    @Autowired
    private ItFileMapper itFileMapper;

    @Autowired
    private FileCollectMapper fileCollectMapper;
    @Override
    public BaseMapper<SupervisorFile, String> getMappser() {
        return itFileMapper;
    }

    /**
     * 查询相关文件
     * @param map
     * @return
     */
    @Override
    public List<SupervisorFile> selectRelated(Map map){
        return itFileMapper.selectRelated(map);
    }

    /**
     * 查询回收站
     * @param map
     * @return
     */
    @Override
    public List<SupervisorFile> selectDelete(Map map){
        return itFileMapper.selectDelete(map);
    }

    /**
     * 判断某用户是否收藏了某文件
     * @param id            文件id
     * @param userId        用户id
     * @return
     */
    @Override
    public boolean checkIfCollec(String id, String userId) {
        FileCollect selectFileCollect = new FileCollect();
        selectFileCollect.setFileId(id);
        selectFileCollect.setUserId(userId);
        return fileCollectMapper.selectCount(selectFileCollect) > 0;
    }

    @Override
    public boolean checkCanUpload(String fileName) {
        String fileTypeByFileName = getFileTypeByFileName(fileName);
        return StringUtils.isNotBlank(fileTypeByFileName);
    }

    /**
     * 通过文件名称获取文件类型
     * @param fileName
     * @return          1为图片  2为视频  3为文档  4为pdf
     */
    public String getFileTypeByFileName(String fileName){
        if(StringUtils.isNotBlank(fileName)){
            String fileSuf = fileName.substring(fileName.lastIndexOf('.')+1);
            if("jpg".equals(fileSuf)||"png".equals(fileSuf)||"gif".equals(fileSuf)||"jpeg".equals(fileSuf)
                    ||"psd".equals(fileSuf)||"bmp".equals(fileSuf)){
                return "1";
            }else if("mp4".equals(fileSuf)||"avi".equals(fileSuf)||"wmv".equals(fileSuf)||"mpg".equals(fileSuf)
                    ||"rm".equals(fileSuf)||"swf".equals(fileSuf)||"mpeg".equals(fileSuf)){
                return "2";
            }else if("doc".equals(fileSuf)||"docx".equals(fileSuf)){
                return "3";
            }else if("pdf".equals(fileSuf)){
                return "4";
            }else if("xls".equals(fileSuf) || "xlsx".equals(fileSuf)){
                return "5";
            }else if("ppt".equals(fileSuf) || "pptx".equals(fileSuf)){
                return "6";
            }
        }
        return "";
    }

    /**
     * 根据id更新pathkey
     * @param id
     * @param key
     */
    @Override
    public void updatePathKeyById(String id, String key) {
        itFileMapper.updatePathKeyById(id,key);
    }

}
